﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using Bigger.Web.WebApi;
using Newtonsoft.Json.Serialization;
using System.Net.Http.Formatting;

namespace Bigger.Study.Web
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API 配置和服务

            // Web API 路由
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            //config.Routes.MapHttpRoute(
            //    name: "DefaultApi",
            //    routeTemplate: "api/{controller}/{action}/{id}",
            //    defaults: new { id = RouteParameter.Optional }
            //);

            //config.Formatters.Clear();
            //config.Formatters.Insert(0, new JavascriptMediaTypeFormatter());
            //config.Formatters.Insert(0, new JsonMediaTypeFormatter());
            //config.Formatters.XmlFormatter.UseXmlSerializer = true;

            //解决 SerializableAttribute 的问题
            config.Formatters.XmlFormatter.UseXmlSerializer = true;
            DefaultContractResolver objDefaultContractResolver = config.Formatters.JsonFormatter.SerializerSettings.ContractResolver as DefaultContractResolver;
            objDefaultContractResolver.IgnoreSerializableAttribute = true;

            config.Filters.Add(new ApiExceptionFilter("WebApiLogAlpha"));
        }
    }
}
